<?php
namespace app\admin\controller;
use app\common\model;
use app\common\model\Source;
use app\common\model\Bankcom;
use app\common\model\Expresscom;

class Shop extends Base
{
    public $configModel;
    public $sourceModel;
    public function __construct () {
        parent::__construct();
        $this->configModel = new model\Config;
        $this->sourceModel = new Source;
    }

    /* 商城轮播 */
    public function swiper () {
        $where = [
            //'type' => 1
        ];
        $list = $this->sourceModel->getAll($where);
        if(!$list) {
            return returnJson(0,'',[]);
        }
        foreach($list as $k=>$v) {
            $list[$k]['picture'] = tomedia($v['url']);
        }
        return returnJson(0,'',$list);
    }

    public function editSwiper () {
        $input = input();
        if(!$input['url']) {
            return returnJson(1,'请填写完整');
        }
        $data = [
            'url' => $input['url'],
            'href' => $input['href'],
            'type' => $input['type']?$input['type']:1,
            'title' => $input['title'],
            'ord' => $input['ord']?$input['ord']:0
        ];
        if($input['id']) {
            $data['id'] = $input['id'];
            $res = $this->sourceModel->isUpdate(true)->save($data);
        }else{
            $res = $this->sourceModel->isUpdate(false)->save($data);
        }
        if($res) {
            return returnJson(0,'保存成功');
        }
        return returnJson(1,'网络错误请重试');
    }

    /* 删除轮播 */
    public function deleteSwiper () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'轮播不存在');
        }
        $this->sourceModel->where('id',$input['id'])->delete();
        return returnJson(0,'删除成功');
    }

    /* 提现银行 */
    public function bankList () {
        $list = Bankcom::getAll(1,'ord desc');
        foreach($list as $k=>$v) {
            $list[$k]['status'] = $v['status']==1?true:false;
        }
        return returnJson(0,'success',$list);
    }

    /* 修改银行状态 */
    public function bankEdit () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'银行不存在');
        }
        $save  = [
            'id' =>$input['id']
        ];
        if(isset($input['status'])) {
            $save['status'] = $input['status'];
        }
       
        $res = Bankcom::update($save);
        return returnJson(0,'已修改');
    }


    public function bankSort () {
        $input = input();
        if(!$input['oldPosition'] || !$input['newPosition']) {
            return returnJson(1,'error');
        }
        $old = Bankcom::getOne(['id'=>$input['oldPosition']]);
        $new = Bankcom::getOne(['id'=>$input['newPosition']]);
       
        //拖动元素的排序号先改掉
        Bankcom::update([
            'ord' => $new['ord']
        ],[
            'id' => $old['id']
        ]);
        if($new['ord']>$old['ord']) {
            $where = "(ord>={$old['ord']} and ord<{$new['ord']}) || id={$new['id']}";
            Bankcom::where($where)->setDec('ord');
        }else{
            $where = "ord>{$new['ord']} and ord<={$old['ord']} || id={$new['id']}";
            Bankcom::where($where)->setInc('ord');
        }
        return returnJson(0,'已修改');
    }

    /* 快递公司 */
    public function expresscomList () {
        $list = Expresscom::getAll(1,'ord desc');
        foreach($list as $k=>$v) {
            $list[$k]['status'] = $v['status']==1?true:false;
        }
        return returnJson(0,'success',$list);
    }

    /* 修改快递公司 */
    public function expresscomEdit () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'快递公司不存在');
        }
        $save  = [
            'id' =>$input['id']
        ];
        if(isset($input['status'])) {
            $save['status'] = $input['status'];
        }
        $res = Expresscom::update($save);
        return returnJson(0,'已修改');
    }


    public function expresscomSort () {
        $input = input();
        if(!$input['oldPosition'] || !$input['newPosition']) {
            return returnJson(1,'error');
        }
        $old = Expresscom::getOne(['id'=>$input['oldPosition']]);
        $new = Expresscom::getOne(['id'=>$input['newPosition']]);
       
        //拖动元素的排序号先改掉
        Expresscom::update([
            'ord' => $new['ord']
        ],[
            'id' => $old['id']
        ]);
        if($new['ord']>$old['ord']) {
            $where = "(ord>={$old['ord']} and ord<{$new['ord']}) || id={$new['id']}";
            Expresscom::where($where)->setDec('ord');
        }else{
            $where = "ord>{$new['ord']} and ord<={$old['ord']} || id={$new['id']}";
            Expresscom::where($where)->setInc('ord');
        }
        return returnJson(0,'已修改');
    }

}
